#!/usr/bin/env python
# -*- encoding: utf-8 -*-
'''
@File    :   NRC22_Phy_test.py
@Time    :   2022/09/02 13:31:28
@Author  :   YangLiang
@Version :   1.0
@Contact :   yangliang@chengtech.com
@License :   (C)Copyright 2022-2025
@Desc    :   None
'''

# here put the import lib
from pickle import TRUE
from pytest_assume.plugin import assume

import pytest
import logging
import time
import requests
from sympy import I
# from sympy import assuming
from commDriver.TS_CAN import *
import allure
import sys
from collections.abc import Iterable

from sqlalchemy import false
sys.dont_write_bytecode = False


CommunicationControl = [([0x28, 0x00, 0x01],[0x7F, 0x28, 0x22],[0x68]),
                        ([0x28, 0x03, 0x01],[0x7F, 0x28, 0x22],[0x68]),
                        ]

@allure.severity('critical')
class Test_SID0x28_NRC22_Phy(object):

    ids = ["Request {}".format(binascii.b2a_hex(bytearray(req), ' '))for req,resp,resp2 in CommunicationControl]
    @allure.story('ECU NRC 22 Test')
    @allure.title('Test NRC22 of 0x28 Service Undervoltage in extended session with Physical Address')
    @allure.description('Test NRC22 of 0x28 Service Undervoltage in extended session with Physical Address')
    @allure.testcase('Test cases address:https://chengtech.pingcode.com/testhub/libraries/630357052177ea2394680877/cases')
    @pytest.mark.parametrize('req,resp,resp2', CommunicationControl, ids=ids)
    def test_SID28_UnderVoltage_Extended_Phy(self, action, Driver,req,resp,resp2):
        iStep = 1
        iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, '3E00', '7E00', 3, iStep, False)
        iStep = Driver['CAN'].SetVolt(Driver, 6, 1, iStep)
        iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, '3E00', '7E00', 0, iStep, False)
        time.sleep(1)
        iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, '3E00', '7E00', 0, iStep, False)
        time.sleep(1)
        iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, req, resp, 0, iStep, False)
        iStep = Driver['CAN'].SetVolt(Driver, 12, 1, iStep)
        iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, req, resp2, 0, iStep, False)
     
     
    ids = ids = ["Request {}".format(binascii.b2a_hex(bytearray(req), ' '))for req,resp,resp2 in CommunicationControl]
    @allure.title('Test NRC22 of 0x28 Service OverVoltage in extended session with Physical Address')
    @allure.description('Test NRC22 of 0x28 Service OverVoltage in extended session with Physical Address')
    @allure.testcase('Test cases address:https://chengtech.pingcode.com/testhub/libraries/630357052177ea2394680877/cases')
    @pytest.mark.parametrize('req,resp,resp2', CommunicationControl, ids=ids)
    def test_SID28_OverVoltage_Extended_Phy(self, action, Driver,req,resp,resp2):
        iStep = 1
        iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, '3E00', '7E00', 3, iStep, False)
        iStep = Driver['CAN'].SetVolt(Driver, 18.5, 1, iStep)
        iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, '3E00', '7E00', 0, iStep, False)
        time.sleep(1)
        iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, '3E00', '7E00', 0, iStep, False)
        time.sleep(1)
        iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, req, resp, 0, iStep, False)
        iStep = Driver['CAN'].SetVolt(Driver, 12, 1, iStep)
        iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, req, resp2, 0, iStep, False)

    ids = ids = ["Request {}".format(binascii.b2a_hex(bytearray(req), ' '))for req,resp,resp2 in CommunicationControl]
    @allure.title('Test NRC22 of 0x28 Service Vspeed over 3kph in extended session with Physical Address')
    @allure.description('Test 0x28 Service Vspeed over 3kph confition response NRC22 in extended session with physical addressing')
    @allure.testcase('Test cases address:https://chengtech.pingcode.com/testhub/libraries/630357052177ea2394680877/cases')
    @pytest.mark.parametrize('req,resp,resp2', CommunicationControl, ids=ids)
    def test_SID28_OverVspeed_Extended_Phy(self, action, Driver,req,resp,resp2):
        iStep = 1
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, [0x10, 0x03], [0x50, 0x03, 0x00, 0x32, 0x01, 0xF4], 0, iStep, False)
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, [0x3E, 0x00], [0x7E, 0x00], 0, iStep, False)
        #设置车速大于3KM/H
        iStep = Driver['CAN'].setSignalValue('abs_sts_run1', 'abs_pm_veh_spd',80,iStep)

        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, req, resp, 0, iStep, False)
        #设置车速为0KM/H
        iStep = Driver['CAN'].setSignalValue('abs_sts_run1', 'abs_pm_veh_spd',0,iStep)
        iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, req, resp2, 0, iStep, False)

    ids = ids = ["Request {}".format(binascii.b2a_hex(bytearray(req), ' '))for req,resp,resp2 in CommunicationControl]
    @allure.story('ECU NRC 22 Test')
    @allure.title('Test NRC22 of 0x28 Service Undervoltage in extended session with Physical Address')
    @allure.description('Test 0x28 Service Undervoltage confition response NRC22 in extended session with physical addressing')
    @allure.testcase('Test cases address:https://chengtech.pingcode.com/testhub/libraries/630357052177ea2394680877/cases')
    @pytest.mark.parametrize('req,resp,resp2', CommunicationControl, ids=ids)
    def test_SID28_UnderVoltage_Extended_Send3E80_Phy(self, action, Driver,req,resp,resp2):
        iStep = 1
        iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, '3E80', '', 3, iStep, False)
        iStep = Driver['CAN'].SetVolt(Driver, 6, 1, iStep)
        iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, '3E80', '', 0, iStep, False)
        time.sleep(1)
        iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, '3E80', '', 0, iStep, False)
        time.sleep(1)
        iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, req, resp, 0, iStep, False)

        iStep = Driver['CAN'].SetVolt(Driver, 12, 1, iStep)
        iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, req, resp2, 0, iStep, False)
     
     
    ids = ids = ["Request {}".format(binascii.b2a_hex(bytearray(req), ' '))for req,resp,resp2 in CommunicationControl]
    @allure.title('Test NRC22 of 0x28 Service OverVoltage in extended session with Physical Address')
    @allure.description('Test NRC22 of 0x28 Service OverVoltage in extended session with Physical Address')
    @allure.testcase('Test cases address:https://chengtech.pingcode.com/testhub/libraries/630357052177ea2394680877/cases')
    @pytest.mark.parametrize('req,resp,resp2', CommunicationControl, ids=ids)
    def test_SID28_OverVoltage_Extended_Send3E80_Phy(self, action, Driver,req,resp,resp2):
        iStep = 1
        iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, '3E80', '', 3, iStep, False)
        iStep = Driver['CAN'].SetVolt(Driver, 18.5, 1, iStep)
        iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, '3E80', '', 0, iStep, False)
        time.sleep(1)
        iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, '3E80', '', 0, iStep, False)
        time.sleep(1)
        iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, req, resp, 0, iStep, False)
        iStep = Driver['CAN'].SetVolt(Driver, 12, 1, iStep)
        iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, req, resp2, 0, iStep, False)


    ids = ids = ["Request {}".format(binascii.b2a_hex(bytearray(req), ' '))for req,resp,resp2 in CommunicationControl]
    @allure.title('Test NRC22 of 0x28 Service Vspeed over 3kph in extended session with Physical Address')
    @allure.description('Test 0x28 Service Vspeed over 3kph confition response NRC22 in extended session with physical addressing')
    @allure.testcase('Test cases address:https://chengtech.pingcode.com/testhub/libraries/630357052177ea2394680877/cases')
    @pytest.mark.parametrize('req,resp,resp2', CommunicationControl, ids=ids)
    def test_SID28_OverVspeed_Extended_Send3E80_Phy(self, action, Driver,req,resp,resp2):
        iStep = 1
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, [0x10, 0x03], [0x50, 0x03, 0x00, 0x32, 0x01, 0xF4], 0, iStep, False)
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, [0x3E, 0x80], [], 0, iStep, False)
        #设置车速大于3KM/H
        iStep = Driver['CAN'].setSignalValue('abs_sts_run1', 'abs_pm_veh_spd',80,iStep)

        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, req, resp, 0, iStep, False)
        #设置车速为0KM/H
        iStep = Driver['CAN'].setSignalValue('abs_sts_run1', 'abs_pm_veh_spd',0,iStep)
        iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, req, resp2, 0, iStep, False)


class Test_SID0x28_NRC22_Fun(object):

    ids = ["Request {}".format(binascii.b2a_hex(bytearray(req), ' '))for req,resp,resp2 in CommunicationControl]
    @allure.story('ECU NRC 22 Test')
    @allure.title('Test NRC22 of 0x28 Service Undervoltage in extended session with Physical Address')
    @allure.description('Test NRC22 of 0x28 Service Undervoltage in extended session with Physical Address')
    @allure.testcase('Test cases address:https://chengtech.pingcode.com/testhub/libraries/630357052177ea2394680877/cases')
    @pytest.mark.parametrize('req,resp,resp2', CommunicationControl, ids=ids)
    def test_SID28_UnderVoltage_Extended_Fun(self, action, Driver,req,resp,resp2):
        iStep = 1
        iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, '3E00', '7E00', 3, iStep, False)
        iStep = Driver['CAN'].SetVolt(Driver, 6, 1, iStep)
        iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, '3E00', '7E00', 0, iStep, False)
        time.sleep(1)
        iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, '3E00', '7E00', 0, iStep, False)
        time.sleep(1)
        iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, req, resp, 0, iStep, False, 'UDSFun')
        iStep = Driver['CAN'].SetVolt(Driver, 12, 1, iStep)
        iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, req, resp2, 0, iStep, False, 'UDSFun')
     
     
    ids = ids = ["Request {}".format(binascii.b2a_hex(bytearray(req), ' '))for req,resp,resp2 in CommunicationControl]
    @allure.title('Test NRC22 of 0x28 Service OverVoltage in extended session with Physical Address')
    @allure.description('Test NRC22 of 0x28 Service OverVoltage in extended session with Physical Address')
    @allure.testcase('Test cases address:https://chengtech.pingcode.com/testhub/libraries/630357052177ea2394680877/cases')
    @pytest.mark.parametrize('req,resp,resp2', CommunicationControl, ids=ids)
    def test_SID28_OverVoltage_Extended_Fun(self, action, Driver,req,resp,resp2):
        iStep = 1
        iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, '3E00', '7E00', 3, iStep, False)
        iStep = Driver['CAN'].SetVolt(Driver, 18.5, 1, iStep)
        iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, '3E00', '7E00', 0, iStep, False)
        time.sleep(1)
        iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, '3E00', '7E00', 0, iStep, False)
        time.sleep(1)
        iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, req, resp, 0, iStep, False, 'UDSFun')
        iStep = Driver['CAN'].SetVolt(Driver, 12, 1, iStep)
        iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, req, resp2, 0, iStep, False, 'UDSFun')

    ids = ids = ["Request {}".format(binascii.b2a_hex(bytearray(req), ' '))for req,resp,resp2 in CommunicationControl]
    @allure.title('Test NRC22 of 0x28 Service Vspeed over 3kph in extended session with Physical Address')
    @allure.description('Test 0x28 Service Vspeed over 3kph confition response NRC22 in extended session with physical addressing')
    @allure.testcase('Test cases address:https://chengtech.pingcode.com/testhub/libraries/630357052177ea2394680877/cases')
    @pytest.mark.parametrize('req,resp,resp2', CommunicationControl, ids=ids)
    def test_SID28_OverVspeed_Extended_Fun(self, action, Driver,req,resp,resp2):
        iStep = 1
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, [0x10, 0x03], [0x50, 0x03, 0x00, 0x32, 0x01, 0xF4], 0, iStep, False)
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, [0x3E, 0x00], [0x7E, 0x00], 0, iStep, False)
        #设置车速大于3KM/H
        iStep = Driver['CAN'].setSignalValue('abs_sts_run1', 'abs_pm_veh_spd',80,iStep)

        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, req, resp, 0, iStep, False, 'UDSFun')
        #设置车速为0KM/H
        iStep = Driver['CAN'].setSignalValue('abs_sts_run1', 'abs_pm_veh_spd',0,iStep)
        iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, req, resp2, 0, iStep, False, 'UDSFun')

    ids = ids = ["Request {}".format(binascii.b2a_hex(bytearray(req), ' '))for req,resp,resp2 in CommunicationControl]
    @allure.story('ECU NRC 22 Test')
    @allure.title('Test NRC22 of 0x28 Service Undervoltage in extended session with Physical Address')
    @allure.description('Test 0x28 Service Undervoltage confition response NRC22 in extended session with physical addressing')
    @allure.testcase('Test cases address:https://chengtech.pingcode.com/testhub/libraries/630357052177ea2394680877/cases')
    @pytest.mark.parametrize('req,resp,resp2', CommunicationControl, ids=ids)
    def test_SID28_UnderVoltage_Extended_Send3E80_Fun(self, action, Driver,req,resp,resp2):
        iStep = 1
        iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, '3E80', '', 3, iStep, False)
        iStep = Driver['CAN'].SetVolt(Driver, 6, 1, iStep)
        iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, '3E80', '', 0, iStep, False)
        time.sleep(1)
        iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, '3E80', '', 0, iStep, False)
        time.sleep(1)
        iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, req, resp, 0, iStep, False, 'UDSFun')

        iStep = Driver['CAN'].SetVolt(Driver, 12, 1, iStep)
        iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, req, resp2, 0, iStep, False, 'UDSFun')
     
     
    ids = ids = ["Request {}".format(binascii.b2a_hex(bytearray(req), ' '))for req,resp,resp2 in CommunicationControl]
    @allure.title('Test NRC22 of 0x28 Service OverVoltage in extended session with Physical Address')
    @allure.description('Test NRC22 of 0x28 Service OverVoltage in extended session with Physical Address')
    @allure.testcase('Test cases address:https://chengtech.pingcode.com/testhub/libraries/630357052177ea2394680877/cases')
    @pytest.mark.parametrize('req,resp,resp2', CommunicationControl, ids=ids)
    def test_SID28_OverVoltage_Extended_Send3E80_Fun(self, action, Driver,req,resp,resp2):
        iStep = 1
        iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, '3E80', '', 3, iStep, False)
        iStep = Driver['CAN'].SetVolt(Driver, 18.5, 1, iStep)
        iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, '3E80', '', 0, iStep, False)
        time.sleep(1)
        iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, '3E80', '', 0, iStep, False)
        time.sleep(1)
        iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, req, resp, 0, iStep, False, 'UDSFun')
        iStep = Driver['CAN'].SetVolt(Driver, 12, 1, iStep)
        iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, req, resp2, 0, iStep, False, 'UDSFun')


    ids = ids = ["Request {}".format(binascii.b2a_hex(bytearray(req), ' '))for req,resp,resp2 in CommunicationControl]
    @allure.title('Test NRC22 of 0x28 Service Vspeed over 3kph in extended session with Physical Address')
    @allure.description('Test 0x28 Service Vspeed over 3kph confition response NRC22 in extended session with physical addressing')
    @allure.testcase('Test cases address:https://chengtech.pingcode.com/testhub/libraries/630357052177ea2394680877/cases')
    @pytest.mark.parametrize('req,resp,resp2', CommunicationControl, ids=ids)
    def test_SID28_OverVspeed_Extended_Send3E80_Fun(self, action, Driver,req,resp,resp2):
        iStep = 1
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, [0x10, 0x03], [0x50, 0x03, 0x00, 0x32, 0x01, 0xF4], 0, iStep, False)
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, [0x3E, 0x80], [], 0, iStep, False)
        #设置车速大于3KM/H
        iStep = Driver['CAN'].setSignalValue('abs_sts_run1', 'abs_pm_veh_spd',80,iStep)

        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, req, resp, 0, iStep, False, 'UDSFun')
        #设置车速为0KM/H
        iStep = Driver['CAN'].setSignalValue('abs_sts_run1', 'abs_pm_veh_spd',0,iStep)
        iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, req, resp2, 0, iStep, False, 'UDSFun')